import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

class InputBorderField extends StatelessWidget {
  final String tip;
  final TextEditingController controller;
  final FocusNode focusNode;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final EdgeInsets margin;
  final double horizontalPadding;

  const InputBorderField({
    Key key,
    this.margin,
    this.keyboardType,
    this.inputFormatters,
    this.tip,
    this.controller,
    this.focusNode,
    this.horizontalPadding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: EdgeInsets.symmetric(
          horizontal: horizontalPadding ?? getDp(35),
        ),
        constraints: BoxConstraints(maxWidth: getDp(240)),
        padding:
            EdgeInsets.symmetric(horizontal: getDp(12), vertical: getDp(7)),
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(
              color: aceGrayColor,
              width: getDp(0.5),
            ),
            borderRadius: BorderRadius.circular(getDp(15))),
        child: InputNameField(
          controller: controller,
          focusNode: focusNode,
          tip: tip,
          keyboardType: keyboardType,
          inputFormatters: inputFormatters,
          margin: margin,
        ));
  }
}

class InputBoxShadowField extends StatelessWidget {
  final String tip;
  final TextEditingController controller;
  final FocusNode focusNode;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final EdgeInsets margin;
  final double maxWidth;
  final double minWidth;
  final double height;

  const InputBoxShadowField({
    Key key,
    this.minWidth,
    this.maxWidth,
    this.margin,
    this.height,
    this.keyboardType,
    this.inputFormatters,
    this.tip,
    this.controller,
    this.focusNode,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        constraints: BoxConstraints(
          maxWidth: maxWidth ?? getDp(240),
          minWidth: minWidth ?? 0.0,
        ),
        padding: EdgeInsets.only(
          left: getDp(15),
          right: getDp(15),
          top: height != null ? getDp(5) : 0,
          bottom: height != null ? getDp(5) : 0,
        ),
        margin: margin,
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.25),
              blurRadius: getDp(22),
              spreadRadius: -getDp(5),
            ),
          ],
          borderRadius: BorderRadius.circular(getDp(15)),
        ),
        child:
            AnimatedBuilder(animation: focusNode, builder: _buildInputField));
  }

  Widget _buildInputField(BuildContext context, Widget child) {
    return InputNameField(
      controller: controller,
      focusNode: focusNode,
      tip: tip,
      keyboardType: keyboardType,
      inputFormatters: inputFormatters,
      margin: margin,
    );
  }
}

class InputNameField extends StatelessWidget {
  final String tip;
  final TextEditingController controller;
  final FocusNode focusNode;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final EdgeInsets margin;

  const InputNameField({
    Key key,
    this.margin,
    this.keyboardType,
    this.inputFormatters,
    this.tip,
    this.controller,
    this.focusNode,
  }) : super(key: key);

  bool isShowFocusStyle(BuildContext context) =>
      focusNode.hasFocus ||
      !FormEditStatusInheritedWidget.ofEnable(context) ||
      controller.text.isNotEmpty;

  @override
  Widget build(BuildContext context) {
    return TextField(
      inputFormatters: inputFormatters,
      controller: controller,
      focusNode: focusNode,
      scrollPadding: EdgeInsets.zero,
      keyboardType: keyboardType,
      style: TextStyle(
        fontSize: getSp(18),
        color: aceBlueColor,
        height: 1,
        fontWeight: FontWeight.bold,
      ),
      decoration: InputDecoration(
          border: InputBorder.none,
          focusedBorder: InputBorder.none,
          disabledBorder: InputBorder.none,
          labelText: tip,
          labelStyle: TextStyle(
              color:
                  isShowFocusStyle(context) ? aceDarkGrayColor : aceGrayColor,
              fontWeight:
                  isShowFocusStyle(context) ? FontWeight.w400 : FontWeight.w700,
              fontSize: isShowFocusStyle(context) ? getSp(20) : getSp(16),
              height: 1)),
    );
  }
}
